﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;


namespace APMS.WebSite.Controllers
{
    public class ChatingController : BaseController
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }

        //PreviewProject/5/chat/6/chating/action/id
        //use "chat",      // Route name


        public ActionResult create(int projectId, int chatId, String chating_detail)
        {
            //throw new NotImplementedException();   
            String username = "admin";
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            ChatInG newChating= new ChatInG();
            newChating.ChatID = chatId;
            newChating.ChatInGDetail = chating_detail;
            newChating.UserName = username;
            newChating.ModifyDate = DateTime.Now;
            newChating.AddDate = DateTime.Now;


            db.ChatInG.Add(newChating);
            db.SubmitChanges();
            return View("test");
        }

        public ActionResult Delete(int projectId, int id)
        {
            //throw new NotImplementedException();
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


            try
            {
                Chat deleteChatroom = db.Chat.Single(p => p.ChatID == id);
                db.Chat.Remove(deleteChatroom);
                db.SubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                System.Console.WriteLine("called on set with zero items");
            }
            ////////////////////// DELETE DATA

            return Index();

            return View("test");
        }



        public ActionResult Update(int projectId, int id)
        {
            // Initialize data required by master page

            ActionResult exception = base.InitializeAction();
            if (exception != null)
                return exception;

            // Ser Window Title
            base.SetTitle("Chatroom | " + id);

            // SELECT * FROM user WHERE user.UserName = '{currentUser}'
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from chat in db.Chat
                where
                    chat.ChatID == id
                select chat;

            // Prepare ViewData
            ManageChatViewData viewData = new ManageChatViewData();
            viewData.CurrentChat = query.First<Chat>();

            //TODO: Handle both POST and GET methods
            if (Request.HttpMethod == Constants.HttpMethod.POST)
            {
                // If POST means UPDATE the value from Request.Form to DB
                // If GET means VIEW the value from DB
                #region Update record back to DB
                viewData.CurrentChat.ChatRoomName = Request.Form["chatroomname"];

                viewData.IsCurrentChatUpdate = true;

                db.SubmitChanges();
                #endregion
            }
            else
            {
                viewData.IsCurrentChatUpdate = false;
            }

            return View("Update", viewData);
        }

        public ActionResult List(int projectId,int chatId)
        {
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from chating in db.ChatInG
                where
                    chating.ChatID == chatId
                select chating;


            ViewData["all"] = query.ToList();

            return View("test");
        }
    }
}
